#!/bin/bash
#author:yuangang
#datetime:2011-08-11
nginx_dir=/usr/local/nginx
php_dir=/usr/local/php
mysql_dir=/usr/local/mysql

function init()
{
LANG=C
yum -y install wget gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
}

function nginx()
{
  if [ ! -f "pcre-8.01.tar.gz" ]; then  
     echo 'pcre-8.01.tar.gz is not fond'  
     exit 0
  fi  
  if [ ! -f "nginx-1.0.4.tar.gz" ]; then
     echo 'nginx-1.0.4.tar.gz is not fond'  
     exit 0
  fi  
  tar xzvf pcre-8.01.tar.gz
  cd pcre-8.01
  ./configure
  make
  make install
  cd ../
  useradd -s /sbin/nologin www
  tar xzvf nginx-1.0.4.tar.gz
  cd nginx-1.0.4
  ./configure --user=www --group=www --prefix=${nginx_dir} --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
  make
  make install
  cd ../
  mkdir ${nginx_dir}/conf/vhost
  mkdir -p /data/httpd/test.shopex.cn
cat >/data/httpd/test.shopex.cn/test.html <<EOF
www.shopex.cn
EOF
  chown -R admin:admin /data/httpd/test.shopex.cn
  /bin/cp -Rf app_config/nginx/nginx.conf ${nginx_dir}/conf/
  /bin/cp -Rf app_config/nginx/pathinfo.conf ${nginx_dir}/conf/
  /bin/cp -Rf app_config/nginx/vhost/test.conf ${nginx_dir}/conf/vhost/
  ${nginx_dir}/sbin/nginx -t
  ${nginx_dir}/sbin/nginx
  echo 'nginx install Success'  
}

function php_lib()
{
  if [ ! -f "libiconv-1.13.1.tar.gz" ]; then
     echo 'libiconv-1.13.1.tar.gz is not fond'  
     exit 0
  fi
  if [ ! -f "libmcrypt-2.5.8.tar.gz" ]; then
     echo 'libmcrypt-2.5.8.tar.gz is not fond'  
     exit 0
  fi
  if [ ! -f "mhash-0.9.9.9.tar.gz" ]; then
     echo 'mhash-0.9.9.9.tar.gz is not fond'  
     exit 0
  fi
  if [ ! -f "mcrypt-2.6.8.tar.gz" ]; then
     echo 'mcrypt-2.6.8.tar.gz is not fond'  
     exit 0
  fi
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../
}

function mysql()
{
useradd -s /sbin/nologin mysql
tar xzvf mysql-5.1.31.tar.gz
cd mysql-5.1.31/
./configure  --prefix=/usr/local/mysql --with-extra-charsets=all --exec-prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/var --sysconfdir=/usr/local/mysql/etc --with-tcp-port=3306 --with-plugins=heap,innobase,blackhole,partition
make
make install
chown -R mysql:mysql /usr/local/mysql
mkdir -p /usr/local/mysql/etc
cp app_config/mysql/my.cnf /usr/local/mysql/etc/
sed -i 's/skip-federated/#skip-federated/g' /usr/local/mysql/etc/my.cnf
chown -R mysql:mysql /usr/local/mysql/var
chmod 755 /usr/local/mysql/var
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
cd ../
rm -rf /usr/local/mysql/var/*
/usr/local/mysql/bin/mysql_install_db --user=mysql --datadir=/usr/local/mysql/var
service mysqld restart
echo 'mysql install suecc!'
}

function php()
{
tar zxvf php-5.2.13.tar.gz
gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d php-5.2.13 -p1
cd php-5.2.13
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-pdo-mysql=/usr/local/mysql
make ZEND_EXTRA_LIBS='-liconv'
make install
/bin/cp -Rf php.ini-dist /usr/local/php/etc/php.ini
/bin/cp -Rf app_config/php/php-fpm.conf /usr/local/php/etc/
cd ../

tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install
cd ../

tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../

tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../
sed -i 's/extension_dir = "\.\/"/extension_dir = "\/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-20060613\/"/g' /usr/local/php/etc/php.ini
echo extension = "memcache.so" >> /usr/local/php/etc/php.ini
echo extension = "pdo_mysql.so" >> /usr/local/php/etc/php.ini
echo extension = "imagick.so" >> /usr/local/php/etc/php.ini
/usr/local/php/sbin/php-fpm restart
}
function python()
{
tar xjvf Python-2.5.4.tar.bz2
cd Python-2.5.4
./configure
make
make install
cd ../
/bin/cp -Rf app_config/python/python2.5/* /usr/local/lib/python2.5/
}

function mongo()
{
tar xzvf mongodb-linux-x86_64-1.8.1.tgz
mv mongodb-linux-x86_64-1.8.1 /usr/local/mongodb
mkdir -p /data/db27017
/usr/local/mongodb/bin/mongod --port 27017 --dbpath /data/db27017 --fork --logpath /data/db27017/mongodb.log --oplogSize 64 --logappend
echo "/usr/local/mongodb/bin/mongod --port 27017 --dbpath /data/db27017 --fork --logpath /data/db27017/mongodb.log --oplogSize 64 --logappend" >> /etc/rc.local
}
function rabbitmq()
{
tar xzvf otp_src_R13B04.tar.gz
cd otp_src_R13B04
./configure
make
make install
cd ../
tar xzvf rabbitmq-server-generic-unix-2.4.1.tar.gz
mv rabbitmq_server-2.4.1 /usr/local/rabbitmq
ln -s /usr/local/rabbitmq/sbin/rabbitmqctl /usr/local/sbin/rabbitmqctl
ln -s /usr/local/rabbitmq/sbin/rabbitmq-env /usr/local/sbin/rabbitmq-env
ln -s /usr/local/rabbitmq/sbin/rabbitmq-server /usr/local/sbin/rabbitmq-server
/usr/local/sbin/rabbitmq-server -detached
echo '/usr/local/sbin/rabbitmq-server -detached' >> /etc/rc.local
}
case "$1" in
       "init")
       init
       ;;
       "nginx")
       nginx
       ;;
       "php_lib")
       php_lib
       ;;
       "mysql")
       mysql
       ;;
       "php")
       php
       ;;
       "python")
       python
       ;;
       "mongo")
       mongo
       ;;
       "rabbitmq")
       rabbitmq
       ;;
       *)
       echo "Usage : `basename $0` { init|nginx|php_lib|mysql|php|python|mongo|rabbitmq }"
       exit 0
esac
